package com.crane.oss.web.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crane.common.core.dto.MultiResponse;
import com.crane.common.core.dto.Response;
import com.crane.oss.sys.bean.request.role.AddPartyRoleRequest;
import com.crane.oss.sys.bean.request.role.QueryPartyRoleRequest;
import com.crane.oss.sys.bean.request.role.UpdatePartyRoleRequest;
import com.crane.oss.sys.bean.resp.PartyRoleBean;
import com.crane.oss.enums.PartyTypeEnum;
import com.crane.oss.sys.service.PartyRoleService;
import com.crane.oss.web.BaseBusinessController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bealon
 * @email ccsu123456@qq.com
 * @date 2019年01月04日 14时54分46秒
 */

@Api(value = "PartyRoleController", tags = {"系统管理-机构角色"})
@RestController
@RequestMapping("/sys/partyRole")
public class PartyRoleController extends BaseBusinessController {

    @Autowired
    private PartyRoleService partyRoleService;

    @ApiOperation(value = "获取列表信息", notes = "获取列表信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryPartyRoleRequest")
    @RequiresPermissions("sys:partyrole:list")
    @PostMapping("/list")
    public MultiResponse<PartyRoleBean> list(@RequestBody QueryPartyRoleRequest request) {
        // 查询列表数据
        IPage<PartyRoleBean> page = partyRoleService.queryList(request);
        return MultiResponse.of(page);
    }

    @ApiOperation(value = "机构类型列表", notes = "获取机构类型列表信息")
    @GetMapping("/partyType")
    public MultiResponse<Map<String, Object>> list() {
        // 查询列表数据
        List<Map<String, Object>> list = new ArrayList<>();
        for (PartyTypeEnum partyTypeEnum : PartyTypeEnum.values()) {
            Map<String, Object> map = new HashMap<>();
            map.put("key", partyTypeEnum.getType());
            map.put("name", partyTypeEnum.getRemark());
            list.add(map);
        }
        return MultiResponse.ofWithoutTotal(list);
    }

    @ApiOperation(value = "新增", notes = "获取新增信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AddPartyRoleRequest")
    @RequiresPermissions("sys:partyrole:save")
    @PostMapping("/save")
    public Response save(@RequestBody @Valid AddPartyRoleRequest request) {
        partyRoleService.save(request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "修改", notes = "修改")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "UpdatePartyRoleRequest")
    @RequiresPermissions("sys:partyrole:update")
    @PostMapping("/update")
    public Response update(@RequestBody @Valid UpdatePartyRoleRequest request) {
        partyRoleService.update(request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "删除", notes = "删除")
    @RequiresPermissions("sys:partyrole:delete")
    @PostMapping("/delete")
    public Response delete(@RequestBody String data) {
        String sid = getParamter(data, "id");
        partyRoleService.delete(Long.parseLong(sid));
        return Response.buildSuccess();
    }

}
